package org.lboot.ufs.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.jpa.utils.JpaDataUtil;
import org.lboot.ufs.entity.UfsFile;
import org.lboot.ufs.params.FileQueryParams;
import org.lboot.ufs.service.UfsCtl;
import org.lboot.ufs.service.UfsFileService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("ufs")
@AllArgsConstructor
@Api(tags = "文件管理")
@ConditionalOnProperty(prefix = "ufs.api",name = "disable",havingValue = "true",matchIfMissing = true)
public class UfsFileController {
    UfsFileService fileService;

    UfsCtl ufsCtl;

    @GetMapping("file")
    @ApiOperation(value = "文件列表",notes = "")
    public ResponseDTO<Object> fileQueryList(FileQueryParams params){
        Pageable pageable = JpaDataUtil.buildPageable(params);
        /*
        构建 Example
         */
        UfsFile ufsFile = new UfsFile();
        BeanUtil.copyProperties(params, ufsFile, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<UfsFile> example = Example.of(ufsFile);
        return ResponseDTO.succData(fileService.getAll(pageable,example));
    }

    @GetMapping("file/{id}")
    @ApiOperation(value = "文件查询")
    public ResponseDTO<Object> fileQuery(@PathVariable("id") String id){
        return ResponseDTO.succData(
                fileService.getOneUnwrap(id)
        );
    }

    @DeleteMapping("file/{id}")
    @ApiOperation(value = "文件删除")
    public ResponseDTO<Object> fileDelete(@PathVariable("id") String id){
        fileService.deleteOne(id);
        return ResponseDTO.succMsg("删除成功");
    }

}
